local CLOGCOLOR = Color( 0, 200, 255, 255 )

if SERVER then

	AddCSLuaFile( "shared.lua" )
	
	print( "Client Logger loaded!" )
	
	local PropCount = 0
	
	local function ClientLogConsole( ply, model, kind )
		
		PropCount = PropCount + 1
		
		if kind then
		
			kind2 = "   "..kind.."   "
			
		elseif kind == nil then kind2 = ""
		
		end
	
		if ply:IsPlayer() then
		
			if ply:GetNetworkedInt( "clientlogger_print" ) == 1 then
			
			--print to console
				for k,v in pairs( player.GetAll() ) do
			
					if v:IsAdmin() then
				
						v:PrintMessage( HUD_PRINTCONSOLE,
						ply:Nick().."   "..ply:SteamID().."   has spawned a"..kind2..model )
				
					end
			
				end
			
			end
			
			for k,v in pairs( player.GetAll() ) do
				--print to vgui
				if v:IsAdmin() then
				
					umsg.Start( "ClientLoggerAddLine", v )
						umsg.String( ply:Nick() )
						umsg.String( ply:SteamID() )
						umsg.String( kind )
						umsg.String( model )
						umsg.Long( PropCount )
					umsg.End()
					
				end
			end
		end
	
	end
	

	concommand.Add( "clientlogger_print", function( ply, cmd, args )
	
		if args[1] == "1" then ply:SetNetworkedInt( "clientlogger_print", 1 )
		
		elseif args[1] == "0" then ply:SetNetworkedInt( "clientlogger_print", 0 )
		
		end
	
	end )

	
	
	/*hook.Add( "PlayerGiveSwep", "ClientLoggerSwep", function( ply, class, weapon )
	
		ClientLogConsole( ply, class ) 
	
	end ) broken for now but sandbox already does log it*/
	
	
	hook.Add( "PlayerSpawnedEffect", "ClientLoggerEffect", function( ply, model, ent )
	
		ClientLogConsole( ply, model, "Effect" )
	
	end )
	
	
	hook.Add( "PlayerSpawnedNPC", "ClientLoggerNPC", function( ply, NPC )
	
		local name = "NPC ERRORFUCK"
		
		name = NPC:GetClass()
	
		ClientLogConsole( ply, name, "NPC" )
	
	end )
	
	
	hook.Add( "PlayerSpawnedProp", "ClientLoggerProp", function( ply, model, ent )
	
		ClientLogConsole( ply, model, "Prop" )
	
	end )
	
	
	hook.Add( "PlayerSpawnedRagdoll", "ClientLoggerRagdoll", function( ply, model, ent )
	
		ClientLogConsole( ply, model, "Ragdoll" )
	
	end )
	
	
	hook.Add( "PlayerSpawnedSENT", "ClientLoggerSENT", function( ply, ent )
	
		local name = "SENT ERRORFUCK"
		
		name = ent:GetClass()
	
		ClientLogConsole( ply, name, "SENT" )
	
	end )
	
	
	hook.Add( "PlayerSpawnedVehicle", "ClientLoggerVehicle", function( ply, ent )
	
		local name = "VEHICLE ERRORFUCK"
		
		name = ent:GetClass()
	
		ClientLogConsole( ply, name, "Vehicle" )
	
	end )
	
	
	hook.Add( "PlayerInitialSpawn", "ClientLoggerBuild", function( ply )

			umsg.Start( "InitClientLogger", ply )
			umsg.End()
			
			umsg.Start( "PrintMessage", ply )
				umsg.String( "Client Logger loaded! Bind a key to +cl_menu "..
				"to access it!" )
			umsg.End()
			
			ply:SetNetworkedInt( "clientlogger_print", 1 )

	end )
	

end




if CLIENT then

	print( "Client Logger loaded!\n" )
	
	
	local function InitializeCLog()
	
		ClientLoggerFrame = vgui.Create( "DFrame" )
		ClientLoggerFrame:SetSize( 700, 500 )
		ClientLoggerFrame:Center()
		ClientLoggerFrame:SetTitle( "Client Logger by Ice Tea" )
		ClientLoggerFrame:SetVisible( false )
		ClientLoggerFrame:SetDraggable( true )
		ClientLoggerFrame:SetVerticalScrollbarEnabled( true )
		ClientLoggerFrame:ShowCloseButton( false )
		ClientLoggerFrame:SetDeleteOnClose( false )
		ClientLoggerFrame:SetMouseInputEnabled( false )
		ClientLoggerFrame:MakePopup()
		
		function ClientLoggerFrame:Think()
		
			ClientLoggerList:SortByColumn( 5, true )
	
		end
		
		
		ClientLoggerDropDown = vgui.Create( "DMultiChoice", ClientLoggerFrame )
		ClientLoggerDropDown:SetPos( 10, 30 )
		ClientLoggerDropDown:SetSize( ClientLoggerFrame:GetWide()-20, 20 )
		ClientLoggerDropDown:SetEditable( false )
		
		
		ClientLoggerList = vgui.Create( "DListView", ClientLoggerFrame )
		ClientLoggerList:SetPos( 10, 60 )
		ClientLoggerList:SetSize( ClientLoggerFrame:GetWide()-20, ClientLoggerFrame:GetTall()-85 )
		ClientLoggerList:SetMultiSelect( false )
		--ClientLoggerList:SetSortable( false ) don't set or use
		ClientLoggerList:SetDrawBackground( true )
		ClientLoggerList:AddColumn( "Name" ):SetFixedWidth( 150 )
		ClientLoggerList:AddColumn( "Steam ID" ):SetFixedWidth( 125 )
		ClientLoggerList:AddColumn( "Class" ):SetFixedWidth( 40 )
		ClientLoggerList:AddColumn( "Item" ):SetFixedWidth( 365 )
		ClientLoggerList:AddColumn( "Count" ):SetFixedWidth( 0 )--Debugging only
		ClientLoggerList.OnRowRightClick = function( panel, line )
		
			local dMenu = DermaMenu()
			
			dMenu:AddOption( "Copy SteamID", function()
			
				SetClipboardText( ClientLoggerList:GetLine( line ):GetValue( 2 ) )
				
			end )
			
			dMenu:Open()
			
		end
		
	
		local CheckBox = vgui.Create( "DCheckBoxLabel", ClientLoggerFrame )
		CheckBox:SetPos( 10, ClientLoggerFrame:GetTall()-20 )
		CheckBox:SetText( "Log to console" )
		CheckBox:SetConVar( "clientlogger_print" )
		CheckBox:SizeToContents()
		
		
		local ClearList = vgui.Create( "DButton", ClientLoggerFrame )
		ClearList:SetPos( ClientLoggerFrame:GetWide()-80, ClientLoggerFrame:GetTall()-23 )
		ClearList:SetSize( 60, 20 )
		ClearList:SetText( "Clear List" )
		ClearList.DoClick = function()
			ClientLoggerList:Clear()
		end
		
	end
	
	usermessage.Hook( "InitClientLogger", InitializeCLog )
	
	
	
	concommand.Add( "+cl_menu", function()
		
		if LocalPlayer():IsAdmin() then
	
			ClientLoggerFrame:SetVisible( true )
			ClientLoggerFrame:SetMouseInputEnabled( true )
		
			ClientLoggerDropDown:Clear()
			ClientLoggerDropDown:AddChoice("All Players! list doesn't work yet")
			for k,v in pairs( player.GetAll() ) do
				ClientLoggerDropDown:AddChoice( v:Nick() )
			end	
			ClientLoggerDropDown:ChooseOptionID( 1 )
			
		else chat.AddText( CLOGCOLOR, "Client Logger is admins only!" )
			
		end
	
	end )
	
	
	concommand.Add( "-cl_menu", function()
		
		if LocalPlayer():IsAdmin() then
	
			ClientLoggerFrame:SetMouseInputEnabled( false )
			ClientLoggerFrame:SetVisible( false )
			
		else chat.AddText( CLOGCOLOR, "Client Logger is admins only!" )
	
		end
		
	end )
	
	
	usermessage.Hook( "ClientLoggerAddLine", function( info )
	
		local Name = info:ReadString();
		local SteamID = info:ReadString();
		local Class = info:ReadString();
		local Item = info:ReadString();
		local Count = info:ReadLong();
		
		--prevent nil errors spaming
		if not ClientLoggerFrame or not ClientLoggerList then
		
			InitializeCLog()
			
			return false
		
		end
		
		ClientLoggerList:AddLine( Name, SteamID, Class, Item, Count )
	
	end )
	
	
	usermessage.Hook( "PrintMessage", function( info )
	
		chat.AddText( CLOGCOLOR, info:ReadString() )
	
	end )
	

end


